package com.chatbot.controller;

import com.chatbot.dto.CharacterChatRequest;
import com.chatbot.dto.CharacterChatResponse;
import com.chatbot.dto.CharacterConversationDTO;
import com.chatbot.security.JwtTokenProvider;
import com.chatbot.service.CharacterChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/character-chat")
public class CharacterChatController {

    @Autowired
    private CharacterChatService characterChatService;

    @Autowired
    private JwtTokenProvider jwtTokenProvider;

    // 创建新对话
    @PostMapping("/conversations")
    public ResponseEntity<CharacterConversationDTO> createConversation(
            Authentication authentication,
            @RequestParam Long characterId,
            @RequestParam String title,
            @RequestParam(required = false) String sceneContext) {
        Long userId = jwtTokenProvider.getUserIdFromAuthentication(authentication);
        CharacterConversationDTO conversation = characterChatService.createConversation(userId, characterId, title, sceneContext);
        return ResponseEntity.ok(conversation);
    }

    // 发送消息
    @PostMapping("/send")
    public ResponseEntity<CharacterChatResponse> sendMessage(
            Authentication authentication,
            @RequestBody CharacterChatRequest request) {
        Long userId = jwtTokenProvider.getUserIdFromAuthentication(authentication);
        CharacterChatResponse response = characterChatService.sendMessage(userId, request);
        return ResponseEntity.ok(response);
    }

    // 获取对话历史
    @GetMapping("/conversations/{conversationId}")
    public ResponseEntity<CharacterConversationDTO> getConversation(
            Authentication authentication,
            @PathVariable Long conversationId) {
        Long userId = jwtTokenProvider.getUserIdFromAuthentication(authentication);
        CharacterConversationDTO conversation = characterChatService.getConversation(userId, conversationId);
        return ResponseEntity.ok(conversation);
    }

    // 获取用户的所有对话
    @GetMapping("/conversations")
    public ResponseEntity<Page<CharacterConversationDTO>> getUserConversations(
            Authentication authentication,
            @RequestParam(required = false) Long characterId,
            Pageable pageable) {
        Long userId = jwtTokenProvider.getUserIdFromAuthentication(authentication);
        Page<CharacterConversationDTO> conversations;
        if (characterId != null) {
            conversations = characterChatService.getUserConversationsByCharacter(userId, characterId, pageable);
        } else {
            conversations = characterChatService.getUserConversations(userId, pageable);
        }
        return ResponseEntity.ok(conversations);
    }

    // 删除对话
    @DeleteMapping("/conversations/{conversationId}")
    public ResponseEntity<Void> deleteConversation(
            Authentication authentication,
            @PathVariable Long conversationId) {
        Long userId = jwtTokenProvider.getUserIdFromAuthentication(authentication);
        characterChatService.deleteConversation(userId, conversationId);
        return ResponseEntity.ok().build();
    }
} 